package agend.client.gui.panels.appointment;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class DxAppointmentPanel extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList listContatti;
	private JLabel labelRemoveInvitati;
	private JLabel labelInvitati,labelAddInvitati;
	private JPanel panelButton;
	private ListCellRenderer listCellRenderer;

	private List<ItemListContacts> contactsSelected;
	private DialogAddPeople addPeople;
	private ListModel listModel;

	private HashMap<String,Object> model;
	private JTextField fieldMinimoNumeroInvitati,fieldMassimoNumeroDiInvitati;

	public DxAppointmentPanel() {

		super();
		initGUI();
		addListener();
		addPeople = new DialogAddPeople(null);
		model = new HashMap<String, Object>();


	}

	private void initGUI() {
		
		
		try {
			
			
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.0, 0.1, 0.1};
			thisLayout.rowHeights = new int[] {58, 7, 7};
			thisLayout.columnWeights = new double[] {0.1};
			thisLayout.columnWidths = new int[] {7};
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(300, 400));
			this.setBackground(new java.awt.Color(255,255,255));
			{
				{

					listModel = new ListModel();
					listCellRenderer = new ListCellRender();
					listContatti = new JList(listModel);
					listContatti.setCellRenderer(listCellRenderer);
					listContatti.setPreferredSize(new java.awt.Dimension(122, 196));
				}
				
				this.add(listContatti, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				panelButton = new JPanel();
				GridBagLayout panelButtonLayout = new GridBagLayout();
				panelButtonLayout.rowWeights = new double[] {0.0, 0.0, 0.1, 0.0, 0.1, 0.0};
				panelButtonLayout.rowHeights = new int[] {25, 7, 20, 7, 20, 7};
				panelButtonLayout.columnWeights = new double[] {0.0, 0.1};
				panelButtonLayout.columnWidths = new int[] {153, 7};
				this.add(panelButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				panelButton.setLayout(panelButtonLayout);
				panelButton.setBackground(new java.awt.Color(255,255,255));
				this.add(listContatti, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				listContatti.setBackground(new java.awt.Color(255,255,255));
				listContatti.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				{
					labelInvitati = new JLabel();
					panelButton.add(labelInvitati, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					labelInvitati.setFont(new java.awt.Font("Segoe UI",1,14));
					labelInvitati.setText("Elenco Invitati");
					labelInvitati.setIcon(new ImageIcon(getClass().getClassLoader().getResource("defaultImages/skype_omino.jpg")));
				}
				{
					labelAddInvitati = new JLabel();
					panelButton.add(labelAddInvitati, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					labelAddInvitati.setText("Aggiungi Invitati");
					labelAddInvitati.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
					labelAddInvitati.setFont(new java.awt.Font("Segoe UI",1,12));
					labelAddInvitati.setIcon(new ImageIcon(getClass().getClassLoader().getResource("defaultImages/skype_add_contact.jpg")));
				}
				{
					labelRemoveInvitati = new JLabel();
					panelButton.add(labelRemoveInvitati, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					labelRemoveInvitati.setText("Rimuovi Invitati");
					labelRemoveInvitati.setFont(new java.awt.Font("Segoe UI",1,12));
					labelRemoveInvitati.setIcon(new ImageIcon(getClass().getClassLoader().getResource("defaultImages/skype_omino.jpg")));
					labelRemoveInvitati.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				}
				{
					JPanel panelNumeroInvitati = new JPanel();
					GridBagLayout panelNumeroInvitatiLayout = new GridBagLayout();
					panelButton.add(panelNumeroInvitati, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					panelNumeroInvitati.setBackground(new java.awt.Color(255,255,255));
					panelNumeroInvitatiLayout.rowWeights = new double[] {0.1};
					panelNumeroInvitatiLayout.rowHeights = new int[] {7};
					panelNumeroInvitatiLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0};
					panelNumeroInvitatiLayout.columnWidths = new int[] {90, 50, 62, 50, 7};
					panelNumeroInvitati.setLayout(panelNumeroInvitatiLayout);
					{
						JLabel labelNumeroInvitati = new JLabel();
						panelNumeroInvitati.add(labelNumeroInvitati, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						labelNumeroInvitati.setText("Numero Invitati");
						labelNumeroInvitati.setFont(new java.awt.Font("Segoe UI",1,10));
					}
					{
						fieldMinimoNumeroInvitati = new JTextField();
						panelNumeroInvitati.add(fieldMinimoNumeroInvitati, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
						fieldMinimoNumeroInvitati.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
						fieldMinimoNumeroInvitati.setBackground(new java.awt.Color(128,255,255));
					}
					{
						fieldMassimoNumeroDiInvitati = new JTextField();
						panelNumeroInvitati.add(fieldMassimoNumeroDiInvitati, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
						fieldMassimoNumeroDiInvitati.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
						fieldMassimoNumeroDiInvitati.setBackground(new java.awt.Color(128,255,255));
					}
				}
			}
			{
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * Pulsante che ci permette di aggiungere le persone alle quali inviare
	 * la richiesta di appuntamento
	 */
	private void addListener(){

		
		labelAddInvitati.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				addPeople.showGui();
				contactsSelected = addPeople.getSelectedEmails();
				listContatti.updateUI();
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseEntered(e);
				labelAddInvitati.setOpaque(true);
				labelAddInvitati.setBackground(new Color(0,0,255));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseExited(e);
				labelAddInvitati.setBackground(Color.white);
			}
			
			
		});
		
		labelRemoveInvitati.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				
				if(!contactsSelected.isEmpty()){
					
					contactsSelected.remove(listContatti.getSelectedValue());
					listContatti.updateUI();
				}
				else 
					JOptionPane.showMessageDialog(null,"Attenzione non ci sono invitati da eliminare ");
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseEntered(e);
				labelRemoveInvitati.setOpaque(true);
				labelRemoveInvitati.setBackground(new Color(0,0,255));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseExited(e);
				labelRemoveInvitati.setBackground(Color.white);
			}
			
			
		});
		
	
	}
	
	private class ListModel extends AbstractListModel<ItemListContacts>
	{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public ItemListContacts getElementAt(int i) {
			// TODO Auto-generated method stub
			if(contactsSelected!=null)
				return contactsSelected.get(i);
			return null;
		}

		@Override
		public int getSize() {
			// TODO Auto-generated method stub
			if(contactsSelected!=null)
				return contactsSelected.size();
			return 0;
		}

	}

	private class ListCellRender implements ListCellRenderer<ItemListContacts>
	{


		@Override
		public Component getListCellRendererComponent(
				JList<? extends ItemListContacts> list, ItemListContacts value,
				int row, boolean isSelected, boolean cellHasFocus) {
			// TODO Auto-generated method stub


			JLabel labelName = new JLabel();
			labelName.setFont(new Font("SogeUi",Font.BOLD,12));
			labelName.setPreferredSize(new Dimension(10,20));
			labelName.setToolTipText(value.getEmail());
			labelName.setOpaque(true);
		
			if(isSelected)
				labelName.setBackground(new Color (0,0,250));
			else
				labelName.setBackground(Color.white);

			labelName.setText(value.getName());

			return labelName;
		}
	}

	/**
	 * 
	 * @return restituisce il modello del pannello
	 */
	public HashMap<String,Object> getModel(){

		String stringNumeroMinimoInvitati = fieldMinimoNumeroInvitati.getText();
		String stringNumeroMassimoInvitati = fieldMassimoNumeroDiInvitati.getText(); 
				
		
		if(!stringNumeroMinimoInvitati.equals(""))
			model.put("NumeroMinimoInvitati",Integer.parseInt(stringNumeroMinimoInvitati));
		else
			model.put("NumeroMinimoInvitati",0);

		
		if(!stringNumeroMassimoInvitati.equals(""))
			model.put("NumeroMassimoInvitati",Integer.parseInt(stringNumeroMassimoInvitati));
		else
			model.put("NumeroMassimoInvitati",0);
		
		if(contactsSelected != null)
		{
			model.put("NumeroContatti",contactsSelected.size());

			for(int i=0;i<contactsSelected.size();i++)

				model.put("Contatto"+i,contactsSelected.get(i));

			return model;
		}
		else 
			return null;

	}

	
	
	public JTextField getFieldMinimoNumeroInvitati() {
		return fieldMinimoNumeroInvitati;
	}


	public JTextField getFieldMassimoNumeroDiInvitati() {
		return fieldMassimoNumeroDiInvitati;
	}

	

	
	
}
